package com.runtime.hongyunuser.service.Impl;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.runtime.hongyunuser.bean.WXPayConfigImpl;
import com.runtime.hongyunuser.service.WechatPayService;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

public class WechatPayServiceImpl implements WechatPayService {
    private static final String APP_ID = "your_appid";
    private static final String MCH_ID = "your_mchid";
    private static final String API_KEY = "your_apikey";

    public String createNativeOrder(String orderId, BigDecimal amount, String description,HttpServletRequest request) {
        WXPay wxpay = new WXPay(new WXPayConfigImpl());
        Map<String, String> data = new HashMap<>();
        data.put("appid", APP_ID);
        data.put("mch_id", MCH_ID);
        data.put("nonce_str", WXPayUtil.generateNonceStr());
        data.put("body", description);
        data.put("out_trade_no", orderId);
        data.put("total_fee", amount.multiply(BigDecimal.valueOf(100)).intValue() + "");
        data.put("spbill_create_ip", request.getRemoteAddr());
        data.put("notify_url", "https://yourdomain.com/notify");
        data.put("trade_type", "NATIVE");

        try {
            Map<String, String> resp = wxpay.unifiedOrder(data);
            if ("SUCCESS".equals(resp.get("return_code"))) {
                return resp.get("code_url"); // 生成二维码
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}